package com.person.webdemoproject2.controller;

import com.person.webdemoproject2.pojo.*;
import com.person.webdemoproject2.service.FindService;
import lombok.extern.slf4j.Slf4j;
import org.apache.juli.logging.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;
@CrossOrigin
@RestController
@Slf4j
@RequestMapping("/find")
public class FindController {
    @Autowired
    FindService

    findService;
    @GetMapping("/userinfo")
        public Result userinfo(@RequestHeader String token){
            log.info("用户查询数据");

           User user = findService.userinfo(token);

            return Result.success(user);
        }
        @GetMapping("/fuser")
    public Result  fuser(@RequestHeader String token){
        log.info("用户查询关注数量");

        Integer count = findService.countf(token);

        return Result.success(count);
    }
    @GetMapping("/fans")
    public Result  fans(@RequestHeader String token){
        log.info("用户查询粉丝数量");

        Integer count = findService.fans(token);

        return Result.success(count);
    }
    @GetMapping("/byusername")
    public Result byusername(String username){
        log.info("用户通过用户名查询其他用户");

        User user = findService.byusername(username);

        return Result.success(user);
    }

    @GetMapping("/fname")
    public Result fname(@RequestHeader String token){
        log.info("用户查询粉丝或关注人");

        List<UserRelationship> userRelationship = findService.fname(token);

        return Result.success(userRelationship);
    }

    @GetMapping("/posts")

    public Result getPosts(@RequestParam String type,@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                           @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end,@RequestParam(defaultValue = "1") int page,
    @RequestParam(defaultValue = "10") int pageSize) {
        if ("time".equals(type)) {
            return Result.success(findService.getPostsByDate(begin,end,page,pageSize)) ;  // 根据日期查询
        } else if ("likes".equals(type)) {
            return Result.success(findService.getPostsByLikes(page,pageSize));  // 根据点赞量查询
        }
        return Result.success("联系管理员");
    }
    @GetMapping("/search")
    public Result search(@RequestParam String keyword){
        log.info("用户利用关键词搜索 {} {}",keyword,findService.getPostsBykeyword(keyword));

        return Result.success(findService.getPostsBykeyword(keyword));
    }
    @GetMapping("/collections")
    public Result collections(@RequestHeader String token){
        log.info("用户查看收藏夹");

        List<Post> collections=findService.collections(token);
        if (collections==null){
            return Result.success("收藏夹无内容");
        }
        return Result.success(collections);
    }
    @GetMapping("/{postId}/comments")
    public Result commenets(@PathVariable Integer postId){
        log.info("用户查看帖子评论 {}",postId);

        List<Comment> comments=findService.comments(postId);
        if (comments==null){
            return Result.success("评论为空");
        }
        return Result.success(comments);
    }
    @GetMapping("/{commentId}/recomments")
    public Result recommenets(@PathVariable Integer commentId,@RequestParam(defaultValue = "1") int page,
                              @RequestParam(defaultValue = "10") int pageSize){
        log.info("用户查看帖子评论 {}",commentId);

        List<reComment> recomments=findService.recomments(commentId,page,pageSize);
        if (recomments==null){
            return Result.success("评论为空");
        }
        return Result.success(recomments);
    }
    @GetMapping("dynamic")
    public Result dynamic(@RequestHeader String token,@RequestParam(defaultValue = "1") int page,
                          @RequestParam(defaultValue = "10") int pageSize){
        log.info("用户查看动态");
        List<Post> Posts=findService.getPostsByfans(token,page,pageSize);
        return Result.success(Posts);
    }
    @GetMapping("/mypost")
    public Result getMyPosts(@RequestHeader String token,@RequestParam(defaultValue = "1") int page,
    @RequestParam(defaultValue = "10") int pageSize){
        log.info("用户查看自己的帖子");
        List<Post> Posts=findService.getPostsByuserid(token,page,pageSize);
        return Result.success(Posts);
    }
    @GetMapping("/mycomment")
    public Result getMyComment(@RequestHeader String token,@RequestParam(defaultValue = "1") int page,
                               @RequestParam(defaultValue = "10") int pageSize){
        log.info("用户查看自己的评论");

        List<Comment> Comments=findService.getCommentByuserid(token,page,pageSize);
        return Result.success(Comments);
    }
    @GetMapping("/{userid}/posts")
    public Result getPosts(@PathVariable Integer userid,@RequestParam(defaultValue = "1") int page,
                           @RequestParam(defaultValue = "10") int pageSize){
        log.info("通过用户id查找帖子");
        List<Post> Posts=findService.GetPostsByuserid(userid,page,pageSize);

        return Result.success(Posts);
    }

    @GetMapping("/posts/{postId}")
    public Result getPostById(@PathVariable Integer postId){
        log.info("通过帖子id查找帖子");

      Post post =  findService.getPostById(postId);

      log.info("帖子图片: {}",post.getImage_url());

      return Result.success(post);
    }
    @GetMapping("/{userId}/ID")
    public Result getUserByID(@PathVariable Integer userId){
        log.info("用户通过用户id查找用户");

       User user = findService.getUserByID(userId);

       return Result.success(user);
    }
    }


